IF OBJECT_ID('dbo.GetSubcontractors') IS NOT NULL
BEGIN
    DROP PROCEDURE dbo.GetSubcontractors
    IF OBJECT_ID('dbo.GetSubcontractors') IS NOT NULL
        PRINT '<<< FAILED DROPPING PROCEDURE dbo.GetSubcontractors >>>'
    ELSE
        PRINT '<<< DROPPED PROCEDURE dbo.GetSubcontractors >>>'
END
go
-- =======================================================================
-- Procedure Name: dbo.GetSubcontractors
--
-- Author:      Kevin Cashman, Parallax Consulting LLC
--
-- Create date: 7/23/2007
--
-- Description: Returns list of Subcontractors from Insight
--
-- Parameters:  None
--              
-- Returns:     INT, 0=Success, -1=Error
--
-- SampleCall:
-- EXECUTE dbo.GetSubcontractors()
--
--------------------------------------------------------------------------
-- Date        Initials  Modification
--------------------------------------------------------------------------
-- 7/23/07     KC        Created
-- 8/15/07     KC        Permissions
-- 10/5/07     KC        Added @Name Param.
-- 10/15/07	   CB        Fixed field reference to name field in where clause
-- =======================================================================
CREATE PROCEDURE dbo.GetSubcontractors
     (@Name      VARCHAR(50)=NULL)
 
AS
BEGIN
     /*  Returned Fields for Dropdown
     --------------------------------
     SubcontractorId           INT
     SubcontractorName         VARCHAR             
     SponsorId                 INT
     */

     -- Initial Settings
     SET NOCOUNT ON
     
     -- Declare Local Variables
     DECLARE   @Error            INT,
               @RowCount         INT
               
     -- Result Query    
     SELECT DISTINCT Id AS SubcontractorId,
               Name AS SubcontractorName,
               AdmSponsorId AS SponsorId
     FROM      dbo.AgrSubContractor
     WHERE     ((RTRIM(Name) LIKE '%' + @Name + '%') OR @Name IS NULL)
     ORDER BY Name
     
     SELECT @Error=@@ERROR,@RowCount=@@ROWCOUNT
     IF @Error!=0
          RETURN -1

     -- Return Success
     RETURN 0
     
END
go

IF OBJECT_ID('dbo.GetSubcontractors') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE dbo.GetSubcontractors >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE dbo.GetSubcontractors >>>'
go

GRANT EXECUTE ON dbo.GetSubcontractors TO InsightReport,Insight
go
